package cn.zymall.order.controller;

import cn.zymall.common.model.Order;
import cn.zymall.order.service.MyWxPayService;
import com.github.binarywang.wxpay.bean.notify.WxPayNotifyResponse;
import com.github.binarywang.wxpay.bean.notify.WxPayOrderNotifyResult;
import com.github.binarywang.wxpay.bean.request.WxPayUnifiedOrderRequest;
import com.github.binarywang.wxpay.bean.result.WxPayOrderQueryResult;
import com.github.binarywang.wxpay.bean.result.WxPayUnifiedOrderResult;
import com.github.binarywang.wxpay.exception.WxPayException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @program: mall
 * @description: 我的微信支付控制器
 * @author: YueNiao
 * @email: 1486617253@qq.com
 * @create: 2020-08-22 17:00
 **/
@RestController
@RequestMapping("/wxpay")
public class MyWxPayController {
	@Autowired
		MyWxPayService myWxPayService;

	/**
	 * 调用微信下单
	 * @param request
	 * @param order
	 * @return
	 * @throws WxPayException
	 */
	@PostMapping("/unifiedOrder")
	public WxPayUnifiedOrderResult unifiedOrder(@RequestBody WxPayUnifiedOrderRequest request,@RequestBody Order order) throws WxPayException {
		return this.myWxPayService.unifiedOrder(request,order);
	}


	/**
	 * 异步回调
	 * @param xmlData
	 * @throws WxPayException
	 */
	@PostMapping("/notify/order")
	public void parseOrderNotifyResult(@RequestBody String xmlData) throws WxPayException {
		this.myWxPayService.wxNotify(xmlData);
	}

	/**
	 * 查询微信订单支付是否成功
	 * @param transactionId
	 * @param outTradeNo
	 * @return
	 * @throws WxPayException
	 */
	@GetMapping("/queryOrder")
	public boolean queryOrder(@RequestParam(required = false) String transactionId,
	                                        @RequestParam(required = false) String outTradeNo)
			throws WxPayException {
		return this.myWxPayService.searchWxOrder(outTradeNo);
	}
}
